/**
 * FileContentManager is a Java based file manager desktop application, it can
 * show, edit and manipulate the content of the files archived inside a zip.
 * 
 * Copyright (C) 2007
 * 
 * Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal
 * [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
 * 
 * FileContentManager is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FileContentManager; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package br.com.jteam.jfcm.gui.adapter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import br.com.jteam.jfcm.gui.PresentationFile;
import br.com.jteam.jfcm.gui.RenderedFileType;

/**
 * <p>
 * Respons&aacute;vel por ser um axuliar na cria&ccedil;&atilde;o do
 * conte&uacute;do espec&iacute;fico para a camada de
 * apresenta&ccedil;&atilde;o, que pode ser TEXTO ou IMAGEM.
 * 
 * @author Rafael Naufal <rafael.naufal@gmail.com>
 * @version $Id$
 */
public class ShowFileContentHelper
{
	/**
	 * <p>
	 * Mapeamento do tipo de arquivo visualiz&aacute;vel para o criador de tipos
	 * de arquivos espec&iacute;ficos, que pode ser TEXTO ou IMAGEM.
	 */
	private static Map<RenderedFileType, FileContent<?>> mapFileTypeToContent =
					new HashMap<RenderedFileType, FileContent<?>>();

	/**
	 * Uma refer&ecirc;ncia para o arquivo contido na
	 * apresenta&ccedil;&atilde;o.
	 */
	private PresentationFile<?> presentationFile;

	/**
	 * <p>
	 * Construtor.
	 * 
	 * @param presentationFile -
	 *            arquivo da apresenta&ccedil;&atilde;o.
	 * @throws IllegalArgumentException -
	 *             caso o arquivo da apresenta&ccedil;&atilde;o seja nulo.
	 */
	public ShowFileContentHelper(PresentationFile<?> presentationFile)
	{
		if (presentationFile == null)
		{
			throw new IllegalArgumentException("PresentationFile cannot be null");
		}
		this.presentationFile = presentationFile;
		mapFileTypeToContent.put(RenderedFileType.TEXT, new TextFileContent());
		mapFileTypeToContent.put(RenderedFileType.IMAGE, new ImageFileContent());
	}

	/**
	 * <p>
	 * Respons&aacute;vel por auxiliar a cria&ccedil;&atilde;o do
	 * conte&uacute;do de um arquivo renderiz&aacute;vel, do tipo IMAGEM OU
	 * TEXTO.
	 * 
	 * @return - conte&uacute;do do arquivo, que pode ser IMAGEM ou TEXTO.
	 * @throws ShowFileContentAdapterException -
	 *             Caso ocorra algum erro de entrada / sa&iacute;da na abertura
	 *             ou leitura do arquivo.
	 */
	public Object createContent() throws ShowFileContentAdapterException
	{
		FileContent<?> fileContent =
						mapFileTypeToContent.get(presentationFile.getRenderedFileType());
		if (fileContent == null)
		{
			throw new IllegalArgumentException("No file type registered for class "
							+ presentationFile	.getRenderedFileType()
												.getClass()
												.getName());
		}
		try
		{
			ZipInputStream zin;
			zin =
							new ZipInputStream(new FileInputStream(presentationFile.getPath()));
			ZipEntry entry;
			Object content = null;
			while ((entry = zin.getNextEntry()) != null)
			{
				if (entry.getName().equals(presentationFile.getName()))
				{
					content = fileContent.generateFileContent(zin);
					break;
				}
				zin.closeEntry();
			}
			zin.close();
			return content;
		} catch (FileNotFoundException e)
		{
			throw new ShowFileContentAdapterException(e);
		} catch (IOException e)
		{
			throw new ShowFileContentAdapterException(e);
		}
	}
}
